// 引入request.js请求工具
import request from "@/utils/request.js";

// 上传图片到OSS
export const uploadToOss = (images) => {
    const formData = new FormData();
    images.forEach(image => {
        formData.append('files', image);
    });
    return request.post('/uploadBatch', formData, {
        headers: {
            'Content-Type': 'multipart/form-data'
        }
    });
};

// 创建检测批次并执行检测（使用图片URL）
export const createBatch = (remark, imageUrls) => {
    const formData = new FormData();
    formData.append('remark', remark || '');
    imageUrls.forEach(url => {
        formData.append('imageUrls', url);
    });

    return request.post('/record/batch/create', formData, {
        headers: {
            'Content-Type': 'multipart/form-data'
        }
    });
};

// 查询所有检测批次
export const getAllBatches = () => {
    return request.get('/record/batch/all');
};

// 查询某次检测批次详情
export const getBatchDetail = (id) => {
    return request.get(`/record/batch/detail/${id}`);
};

// 删除检测批次及其所有图片结果
export const deleteBatch = (id) => {
    return request.delete(`/record/batch/delete/${id}`);
};

// 生成报告（流式响应）
export const getReport = (taskBatchId, token) => {
    return fetch(`/api/record/report?taskBatchId=${taskBatchId}`, {
        method: 'GET',
        headers: {
            'Authorization': token ? token : '',
        }
    });
};

// 创建检测批次并获取报告（合并接口）
export const createBatchAndGetReport = (remark, imageUrls) => {
    const formData = new FormData();
    formData.append('remark', remark || '');
    imageUrls.forEach(url => {
        formData.append('imageUrls', url);
    });

    return request.post('/record/batch/analyse', formData, {
        headers: {
            'Content-Type': 'multipart/form-data'
        }
    });
};